package com.jielihaofeng.chat.dao;

import com.jielihaofeng.chat.entity.ChatMessageEntity;
import com.jielihaofeng.framework.common.dao.BaseDao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* 消息表
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2022-07-22
*/
@Mapper
public interface ChatMessageDao extends BaseDao<ChatMessageEntity> {

    @Select({"<script>" +
                "select * from ( " +
                    "select * from chat_message where `type` = '0' and receiver = #{userId} order by create_time desc limit 1 " +
                ") as cm1 " +
                "union all " +
                "select * from ( " +
                    "select * from chat_message where `type` = '0' and sender = #{userId} order by create_time desc limit 1 " +
                ") as cm2 " +
                "union all " +
                "select * from ( " +
                    "select * from chat_message where `type` = '1' and receiver in " +
                    "<foreach collection='groupIds' item='groupId' open='(' separator=',' close=')'>" +
                        "#{groupId}" +
                    "</foreach>" +
                    "order by create_time desc limit 1 " +
                ") as cm3 " +
            "</script>"})
    List<ChatMessageEntity> getMessage(@Param("userId") Long userId, @Param("groupIds") List<Long> groupIds);

}